package com.bomb.disruptor.db;

import com.lmax.disruptor.EventHandler;
import com.lmax.disruptor.WorkHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.atomic.AtomicInteger;

public class InsertHandler implements EventHandler<Sql>, WorkHandler<Sql> {

    private static final Connection connection = DbUtils.getConnection();

    private String id;

    private static AtomicInteger count = new AtomicInteger(0);

    public InsertHandler(String id) {
        this.id = id;
    }

    @Override
    public void onEvent(Sql s, long l, boolean b) throws Exception {
        this.onEvent(s);
    }

    @Override
    public void onEvent(Sql s) throws Exception {
        System.out.println(id + "到了" + s.getIndex());

        try {
            Statement st = connection.createStatement();
            st.execute(s.getInsertSql());
        } catch (SQLException e) {
            if (s.getIndex() % 1000 == 0) {
                System.out.println(id + "异常[" + e.getMessage() + "]：" + s.getInsertSql());
            }
        }
    }


}
